﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Oracle.ManagedDataAccess.Client;
using System;
using System.IO;

namespace Gaea.Oracle
{
    public class GaeaOraclePower : GaeaBasicPower
    {
        private const string _jsonFileName = "appsettings.json";
        private const string _connectionName = "Oracle";


        public static IServiceCollection Register(IServiceCollection serviceCollection, string connectionString)
        {
            serviceCollection.AddGaea<OracleConnection>(new GaeaOracleAdapter());
            return serviceCollection;
        }

        public static IServiceProvider Register(IServiceProvider serviceProvider, string connectionString, string connectionName = _connectionName, MultipleConnectionType multipleConnectionType = MultipleConnectionType.Default)
        {
            return serviceProvider.AddGaeaOption(() =>
            {
                return new GaeaOption()
                {
                    ConnectionName = connectionName,
                    ConnectionString = connectionString,
                    GaeaNameSymbo = new string[] { "`" },
                    BaseTypeGaeaPower = typeof(GaeaOraclePower),
                    ConnectionType = multipleConnectionType,
                    TypeOfDbConnection = typeof(OracleConnection)
                };
            }).AddGaeaOption(() =>
            {
                return new GaeaOption()
                {
                    ConnectionName = connectionName,
                    ConnectionString = connectionString,
                    GaeaNameSymbo = new string[] { "`" },
                    BaseTypeGaeaPower = typeof(GaeaOraclePower<>),
                    ConnectionType = multipleConnectionType,
                    TypeOfDbConnection = typeof(OracleConnection)
                };
            });
        }

        public static IServiceProvider Register(string connectionString, string connectionName = _connectionName, MultipleConnectionType multipleConnectionType = MultipleConnectionType.Default)
        {
            var serviceCollection = new ServiceCollection();
            Register(serviceCollection, connectionString);
            return Register(serviceCollection.BuildServiceProvider(), connectionString, connectionName, multipleConnectionType);
        }


        public static void Register(string connectionStringConfigureName = "Oracle")
        {
            var connectionString = string.Empty;
            var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile(_jsonFileName).Build();
            connectionString = builder.GetConnectionString(_connectionName);
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new Exception($"Load connection string fail!Please check [{_connectionName}] connection string node in {_jsonFileName}");
            }
            Register(connectionString, connectionStringConfigureName);
        }

        public static void Register(IConfigurationRoot configurationRoot, string connectionName, string connectionStringConfigureName = "Oracle")
        {
            connectionName = string.IsNullOrWhiteSpace(connectionName) ? _connectionName : connectionName;
            var connectionString = configurationRoot.GetConnectionString(connectionName);
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new Exception($"Load connection string fail!Please check [{connectionName} name in connectionString node");
            }
            var serviceCollection = new ServiceCollection();
            Register(connectionString, connectionStringConfigureName);
        }

        public static void Register(IConfigurationRoot configurationRoot)
        {
            Register(configurationRoot, _connectionName);
        }

    }
}
